package br.com.tecnoria.controllers
{
	import br.com.tecnoria.vo.FileVO;
	
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;
	
	import org.swizframework.utils.services.ServiceHelper;
	
	public class FileController
	{
		[Inject("fileRemote")]
		public var remote:RemoteObject;
		
		[Inject]
		public var serviceHelper:ServiceHelper;
		
		[Bindable]
		public var file:FileVO;
		
		
		
		/**
		 * Call Function
		 */
		
		[EventHandler(event="FileEvent.SAVE", properties="file")]
		public function saveFile(file:FileVO):void
		{
			serviceHelper.executeServiceCall(remote.saveFile(file), onSaveFile, onFault);
		}
		
		[EventHandler(event="FileEvent.DELETE", properties="file")]
		public function deleteFile(file:FileVO):void
		{
			serviceHelper.executeServiceCall(remote.deleteFile(file), onDeleteFile, onFault);
		}
		
		
		
		/**
		 * Result Function
		 */
		
		public function onSaveFile(result:ResultEvent):void
		{
			//Alert.show("Arquivo salvo com sucesso!");
			file = new FileVO(result.result);
		}
		
		public function onDeleteFile(result:ResultEvent):void
		{
			//Alert.show("Arquivo removido com sucesso!");
		}
		
		
		
		/**
		 * Fault Function
		 */
		
		public function onFault(result:FaultEvent):void
		{
			Alert.show(result.fault.toString(), "Erro");
		}
	}
}